//
//  NSDate+Extension.swift
//  ZuoGJByOwnerSide
//
//  Created by lantaiyuan on 2017/9/4.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//   关于时间的extension

import Foundation

extension NSDate {

    /*
      获取当天日期---年月日时分秒星期
     */
    @objc static func getTodayDate() -> [Int] {
        
        var timers: [Int] = [] //  返回的数组
        let calendar = Calendar.init(identifier: .gregorian)
        var comps = DateComponents.init()
        comps = calendar.dateComponents([.era,.year,.month,.day,.weekday,.hour,.minute,.second], from: Date.init())
        timers.append(comps.year! % 2000)  // 年 ，后2位数
        timers.append(comps.month!)            // 月
        timers.append(comps.day!)                // 日
        timers.append(comps.hour!)               // 小时
        timers.append(comps.minute!)            // 分钟
        timers.append(comps.second!)            // 秒
        timers.append(comps.weekday! - 1)      //星期
        return timers
    }
    
    /*
     计算剩余时间----字符串
     */
    @objc static func compareFromDateStrToDateStr(from:String,to:String) -> Int {
        
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        let timeNumber = Int(formatter.date(from: to)!.timeIntervalSince1970 - formatter.date(from: from)!.timeIntervalSince1970)
        return timeNumber
    }
    /*
     计算当前剩余时间
     */
    @objc static func compareFromDateToDate(from:NSDate,to:NSDate) -> String {
        //获取日历
        let calendar = Calendar.current
        let fromComponents = calendar.dateComponents([.year,.month, .day, .hour,.minute,.second], from: from as Date)
        let toComponents = calendar.dateComponents([.year,.month, .day, .hour,.minute,.second], from: from as Date)
        let dateComponentsFormatter = DateComponentsFormatter()
        dateComponentsFormatter.unitsStyle = .full
        dateComponentsFormatter.allowedUnits = [.year,.month,.day,.hour,.minute,.second]
        let compon = calendar.dateComponents([Calendar.Component.day,Calendar.Component.hour,Calendar.Component.minute,Calendar.Component.second], from: fromComponents, to: toComponents)
        let string = "剩余\(Int(compon.hour!))小时\(Int(compon.minute!))分钟"
        return string
    }
    /*
     按一定格式显示时间
     */
    @objc static func dateWithStringWithformatter(dateStr:String,formatterStr:String) -> String {
        // 格式化时间
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        let date = formatter.date(from: dateStr)
        formatter.dateFormat = formatterStr
        let timeStr = formatter.string(from: date!)
        return timeStr
    }
    /*
     返回小时分秒
     */
    @objc static func timeformatFromSeconds(seconds:NSInteger) -> String {
        //format of hour
        let str_hour = "\(Int(seconds / 3600))"
        //format of minute
        let str_minute = "\(Int(seconds % 3600) / 60)"
        //format of second
        let str_second = "\(Int(seconds % 60))"
        
        //format of time
        let format_time = "\(str_hour):\(str_minute):\(str_second)"
        return format_time
    }
    /*
     返回小时分钟秒---按条件判断输出
     */
    @objc static func timeformatForCNFromSeconds(seconds:NSInteger) -> String {
        //format of hour
        let str_hour = "\(Int(seconds / 3600))"
        //format of minute
        let str_minute = "\(Int(seconds % 3600) / 60)"
        //format of second
        let str_second = "\(Int(seconds % 60))"
        var format_time = "\(str_hour):\(str_minute):\(str_second)"
        
        if str_hour == "0" {//小于1小时
            if str_minute == "0" {//小于1分钟
                if str_second == "0" {
                    format_time = ""
                }
                format_time = "(str_second)秒"
            } else {
                format_time = "\(str_minute)分\(str_second)秒"
            }
        } else {
            format_time = "\(str_hour):\(str_minute):\(str_second)"
        }
        return format_time
    }
    /*
     根据时间戳获取距离现在的状态(刚刚,分钟前,今天,昨天)
     */
    @objc static func distanceTimeWithBeforeTime(beTime:Double) -> String {
        
        var distanceStr:String
        
        //获取当前时间
        let now = NSDate.init().timeIntervalSince1970
        let distanceTime = now - beTime
        
        
        let beDate = NSDate.init(timeIntervalSince1970: beTime)
        let df = DateFormatter.init()
        df.dateFormat = "HH:mm"
        let timeStr = df.string(from: beDate as Date)
        
        df.dateFormat = "dd"
        let nowDay = df.string(from: NSDate.init() as Date)
        let lastDay = df.string(from: beDate as Date)
        
        if distanceTime < 60 {//小于一分钟
            distanceStr = "刚刚"
        } else if distanceTime < 3600{//时间小于一个小时
            distanceStr = "\(distanceTime / 60)分钟前"
        }else if distanceTime < 24 * 60 * 60 && Int(nowDay) == Int(lastDay) {//时间小于一天
            distanceStr = "今天 \(timeStr)"
        }else if distanceTime < 24 * 60 * 60 * 2 && Int(nowDay) != Int(lastDay) {
            
            if (Int(nowDay)! - Int(lastDay)! == 1) || Int(lastDay)! - Int(nowDay)! > 10 && Int(lastDay)! == 1 {
                distanceStr = "昨天\(timeStr)"
            } else {
                df.dateFormat = "MM-dd HH:mm"
                distanceStr = df.string(from: beDate as Date)
            }
        }else if distanceTime < 24 * 60 * 60 * 365 {
            df.dateFormat = "MM-dd HH:mm"
            distanceStr = df.string(from: beDate as Date)
        }else {
            
            df.dateFormat = "yyyy-MM-dd HH:mm"
            distanceStr = df.string(from: beDate as Date)
        }
        return distanceStr
    }
    
    /*
     获取当前的时间
     */
    @objc static func getCurrentTime() -> String {
        let formatter = DateFormatter.init()
        // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
        formatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
        let datenow = NSDate.init()
        
        let currentTimeString = formatter.string(from: datenow as Date)
        return currentTimeString
    }
    /*
     时间转时间戳
     */
    @objc static func toolsTimestampWithTimeStr(timeStr:String) -> String {
        let timeString = timeStr
        
        let formatter = DateFormatter.init()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        formatter.dateFormat = "YYYY-MM-dd HH:mm:ss" // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
        //设置时区,这个对于时间的处理有时很重要
        formatter.timeZone = TimeZone.current
        formatter.locale = NSLocale.current
        
        let date = formatter.date(from: timeString)//-----将字符串按formatter转成nsdate
        //时间转时间戳的方法:
        let timeSp = "\(Double((date?.timeIntervalSince1970)!))"
        return timeSp;
    }
    
    /**
     时间戳转时间
     @param timeString 1498208338000
     @return 2017-06-27
     */
    @objc static func toolsTimeStrWithTimestamp(timeString:String) -> String {
        let formatter = DateFormatter.init()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        formatter.dateFormat = "YYYY-MM-dd HH:mm:ss" // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
        //设置时区,这个对于时间的处理有时很重要
        formatter.timeZone = TimeZone.current
        formatter.locale = NSLocale.current
        // 毫秒值转化为秒
        let date = NSDate.init(timeIntervalSince1970: Double(timeString)! / 1000)
        
        let dateStr = formatter.string(from: date as Date)
        return dateStr
    }
  
    ///日期处理--后台给的是全位数（20161112190000） -> 2016-11-12 19:00:00
   @objc static func toolsChangeDataStyle(toFullStyle string: String) -> String{
    // 格式化时间
    let dateFormat = DateFormatter()
    dateFormat.locale = NSLocale.current
    dateFormat.dateFormat = "YYYY-MM-dd HH:mm:ss"
        let date = dateFormat.date(from: string)
        let dateMatt = DateFormatter()
        dateMatt.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let str = dateMatt.string(from: date ?? Date())
        return str
    }
    /**
     时间戳转小时分钟
     
     @param timeString 1498208338000
     @return 07:00
     */
    @objc static func getHourMinuteWithString(timeString:String) -> String {
        // 格式化时间
        let formatter = DateFormatter.init()
        formatter.locale = Locale.current
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        formatter.dateFormat = "HH:mm"
        // 毫秒值转化为秒
        let date = NSDate.init(timeIntervalSince1970: Double(timeString)!) as NSDate
        let dateString = formatter.string(from: date as Date)
        return dateString
    }
    /**
     时间戳转分钟
     @param timeString 1498208338000
     @return 07:00
     */
    @objc static func getMinuteWithString(timeString:String) -> String {
        // 格式化时间
        let formatter = DateFormatter.init()
        formatter.locale = Locale.current
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        formatter.dateFormat = "mm"
        // 毫秒值转化为秒
        let date = NSDate.init(timeIntervalSince1970: Double(timeString)!) as NSDate
        let dateString = formatter.string(from: date as Date)
        return dateString
    }
}
